<?php 

function write_ini_file($assoc_arr, $path, $has_sections=FALSE) { 
    $content = ""; 
    if ($has_sections) { 
        foreach ($assoc_arr as $key=>$elem) { 
            $content .= "[".$key."]\n"; 
            foreach ($elem as $key2=>$elem2) { 
                if(is_array($elem2)) 
                { 
                    for($i=0;$i<count($elem2);$i++) 
                    { 
                        $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                    } 
                } 
                else if($elem2=="") $content .= $key2." = \n"; 
                else $content .= $key2." = \"".$elem2."\"\n"; 
            } 
        } 
    } 
    else { 
        foreach ($assoc_arr as $key=>$elem) { 
            if(is_array($elem)) 
            { 
                for($i=0;$i<count($elem);$i++) 
                { 
                    $content .= $key."[] = ".$elem[$i]."\n"; 
                } 
            } 
            else if($elem=="") $content .= $key."=\n"; 
            else $content .= $key."=".$elem."\n"; 
        } 
    } 

    if (!$handle = fopen($path, 'w')) { 
        return false; 
    } 
    if (!fwrite($handle, $content)) { 
        return false; 
    } 
    fclose($handle); 
    return true; 
}


$ano = $_POST['temp'];
$etp = $_POST['etapa'];	
$cat = $_POST['cat'];
$ronda = $_POST['ronda'];
$partido = (int)$_POST['partido'];
$win = $_POST['win'];

$p1 = $partido * 2;
$p2 = $p1 -1;

//echo $p x 2;




if(!file_exists('Torneos/'.$ano.'/'.$etp.'/draw'.$cat.'.ini'))
  {
  	die("Cuadro no disponible por el momento.");
  }else{

    $ini = parse_ini_file('Torneos/'.$ano.'/'.$etp.'/draw'.$cat.'.ini'); 
    
	if($win == '1'){
		//echo '<b>'.$ini[$ronda.'-'.$p1] .'</b>';
		$nombre = $ini[$ronda.'-'.$p1];
		$ini[$ronda.'-'.$p1] = '<b>'.$nombre.'</b>';
	}else{
		$nombre2 = $ini[$ronda.'-'.$p2];
		$ini[$ronda.'-'.$p2] = '<b>'.$nombre2.'</b>';
		//echo '<b>'.$ini[$ronda.'-'.$p2] .'</b>';
	}
	$ini[$ronda.'-'.$p1.'-1'] = (int)$_POST['11'];
	$ini[$ronda.'-'.$p1.'-2'] = $_POST['12'];
	$ini[$ronda.'-'.$p1.'-3'] = $_POST['13'];
	$ini[$ronda.'-'.$p2.'-1'] = $_POST['21'];
	$ini[$ronda.'-'.$p2.'-2'] = $_POST['22'];
	$ini[$ronda.'-'.$p2.'-3'] = $_POST['23'];
    /*$ini['1-1-1'] = $_POST['11'];
    $ini['1-1-2'] = $_POST['12'];
    $ini['1-1-3'] = $_POST['13'];*/
	$exito = write_ini_file($ini, 'Torneos/'.$ano.'/'.$etp.'/draw'.$cat.'test.ini', false);
	if($exito)
		echo "Carga completa";
	else echo "Error!!";
  }
?>